#include "medialocator.h"
#include "database.h"

MediaLocator::MediaLocator() {
    listenedFolders=new QList<MediaFolder *>();
    files= new QList<MediaFile *>();
    extensions=new QStringList();
    QSqlQueryModel * model= Querier::getModel("SELECT * FROM ListenedFolders");
    for(int i=0; i<model->rowCount(); i++) {
        MediaFolder * folder=new MediaFolder(model->record(i).value("folderID").toLongLong(),model->record(i).value("path").toString());
        //qDebug() <<"folderID: " << folder->folderID;
        QDir directory=QDir(folder->path);
        if(folder->path.startsWith(".")) {
            directory=QDir(qApp->applicationDirPath());
            #if defined(Q_OS_WIN)
                    directory.cdUp();
            #elif defined(Q_OS_MAC)
                    directory.cdUp();
                    directory.cdUp();
                    directory.cdUp();
            #endif
            qDebug()<<directory.absolutePath();
            QString path=folder->path;
            int start=path.indexOf("/");
            path=path.mid(start+1);

            directory.cd(path);
            folder->path=directory.absolutePath();
            qDebug()<<"adjusted path is "<<folder->path;
        }
        listenedFolders->append(folder);
    }

    model= Querier::getModel("SELECT * FROM Extensions");
    for(int i=0; i<model->rowCount(); i++) {
        extensions->append("*"+model->record(i).value("extension").toString());
        qDebug()<<extensions->at(i);
    }

    foreach(MediaFolder * folder, *listenedFolders) {
        QString query="SELECT * FROM MediaFiles mf WHERE mf.folderID='"+(folder->folderID);
        query+="'";
        QSqlQueryModel * model= Querier::getModel(query);
        if(model) {
            for(int i=0; i<model->rowCount(); i++) {
                MediaFile * file=new MediaFile();
                file->mediaID=model->record(i).value("mediaID").toInt();
                file->shortName=model->record(i).value("fileName").toString();
                //file->coverArt=model->record(i).value("coverArt").toString();
                qDebug()<<"using "<<folder->path;
                file->fileLocation=folder->path;
                files->append(file);
            }
        }
    }

}

void MediaLocator::unlock() {
    mutex->unlock();
    condition.wakeAll();
}

void MediaLocator::run() {
    QList<long long> validIDs;
    QString valid="";
    mutex=new QMutex();
    forever {
        mutex->lock();
        foreach(MediaFolder * folder, * listenedFolders) {
            qDebug()<<"folder: "<<folder->path;
            QDir directory=QDir(folder->path);
            qDebug()<<directory.exists();
            if(directory.exists()) {
                QStringList files=directory.entryList(*extensions,QDir::AllEntries, QDir::Unsorted);
                qDebug() <<files;
                foreach(QString file, files) {
                    qDebug()<<"folder: "<<directory.path()<<"file: "<<file;
                    MediaFile * mediaFile=new MediaFile();
                    mediaFile->fileLocation=folder->path;
                    mediaFile->shortName=file;
                    mediaFile->folderID=folder->folderID;
                    if(valid.length()>0) {
                        valid+=", ";
                    }
                    valid+="'";
                    valid+=QString::number(Querier::addMediaFile(mediaFile));
                    valid+="'";
                    this->files->append(mediaFile);
                }
            }
        }
        qDebug()<<valid;
        Querier::getResult("UPDATE MediaFiles SET accessible='0' WHERE mediaID NOT IN ("+valid+")");
        Querier::getResult("UPDATE MediaFiles SET accessible='1' WHERE mediaID IN ("+valid+")");
        //wait two minutes and scan again
        condition.wait(mutex,2*1000.0*60.0);
        mutex->unlock();
    }
}
